import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from db import db


# 录入员工信息
class InsertFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        # 获取插入的数据变量
        self.id = tk.StringVar()
        self.name = tk.StringVar()
        self.price = tk.StringVar()
        self.work = tk.StringVar()
        self.phone = tk.StringVar()
        self.time = tk.StringVar()

        # 页面布局
        tk.Label(self).grid(row=0, column=0)
        # pady是上下边距
        tk.Label(self, text='工号: ').grid(row=1, column=1, pady=10)
        self.e1 = tk.Entry(self, textvariable=self.id)
        self.e1.grid(row=1, column=2)
        tk.Label(self, text='姓名: ').grid(row=2, column=1, pady=10)
        self.e2 = tk.Entry(self, textvariable=self.name)
        self.e2.grid(row=2, column=2)

        tk.Label(self, text='薪资: ').grid(row=3, column=1, pady=10)
        self.e3 = tk.Entry(self, textvariable=self.price)
        self.e3.grid(row=3, column=2)

        tk.Label(self, text='部门: ').grid(row=4, column=1, pady=10)
        self.e4 = tk.Entry(self, textvariable=self.work)
        self.e4.grid(row=4, column=2)

        tk.Label(self, text='电话: ').grid(row=5, column=1, pady=10)
        self.e5 = tk.Entry(self, textvariable=self.phone)
        self.e5.grid(row=5, column=2)

        tk.Label(self, text='入职时间: ').grid(row=6, column=1, pady=10)
        self.e6 = tk.Entry(self, textvariable=self.time)
        self.e6.grid(row=6, column=2)


        tk.Button(self, text='录入', command=self.create_json).grid(row=7, column=2, sticky=tk.E)

    # 清空按钮的内容
    def entry_clear(self):
        # entry.delete(0, tk.END)
        self.e1.delete(0, tk.END)
        self.e2.delete(0, tk.END)
        self.e3.delete(0, tk.END)
        self.e4.delete(0, tk.END)
        self.e5.delete(0, tk.END)
        self.e6.delete(0, tk.END)

    def create_json(self):
        self.id = self.e1.get()
        self.name = self.e2.get()
        self.price = self.e3.get()
        self.work = self.e4.get()
        self.phone = self.e5.get()
        self.time = self.e6.get()
        if self.id == "" or self.name == "" or self.price == "" or self.work == "" or self.phone == "" or self.time == "":
            messagebox.showerror('提示信息', '输入信息不完整,无法登记')
            return


        db.create_json('staff.json')
        db.json_read('staff.json')

        t = db.json_load
        d = {'id': self.id, 'name': self.name, 'price': self.price, 'work': self.work, 'phone': self.phone, 'time': self.time}

        for i in t:
            if i['id'] == d['id']:
                tk.messagebox.showinfo("提示信息", "该工号已被登记")
                return
        d = {'id': self.id, 'name': self.name, 'price': self.price, 'work': self.work, 'phone': self.phone, 'time': self.time}
        db.json_insert('staff.json', d)
        tk.messagebox.showinfo("提示信息", "登记成功")
        self.entry_clear()


# 查询员工信息
class SelectFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)

        colum_names = ['工号', '姓名', '薪资', '部门', '电话', '入职时间']
        self.tree_view = ttk.Treeview(self, show='headings', columns=colum_names)

        # 设置表头
        for colum in colum_names:
            self.tree_view.heading(column=colum, text=colum)

        self.tree_view.tag_configure('even', background='green')  # 声明奇数行的属性
        self.tree_view.tag_configure('odd', background='white')  # 声明偶数行的属性
        # 设置每列的宽度,列中的内容居中
        self.tree_view.column(column=0, width=80, anchor='center')
        self.tree_view.column(column=1, width=80, anchor='center')
        self.tree_view.column(column=2, width=80, anchor='center')
        self.tree_view.column(column=3, width=80, anchor='center')
        self.tree_view.column(column=4, width=80, anchor='center')
        self.tree_view.column(column=5, width=80, anchor='center')
        self.tree_view.pack()
        self.show_clear()
        tk.Button(self, text='刷新', command=self.show_data).pack(side='right')

    # 清空表格中的信息
    def show_clear(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)

    # 刷新按钮的功能
    def show_data(self):
        # 每次刷新前先清空一下表格的信息
        self.show_clear()
        #取出学生信息
        db.json_read('staff.json')
        index = 0
        for row in db.json_load:
            t = []
            my_tag = 'odd'
            if index % 2 == 0:
                my_tag = 'even'
            for key, value in row.items():
                t.append(value)
            # Treeview中无法拆解字典中的元素,因为字典或元组,是有序的可迭代集合,而字典是一种不可迭代的无序的键值对集合,通过键来访问对应的值，无法像元组和字典那样通过下标迭代访问
            self.tree_view.insert('', "end", values=t, tags=my_tag)
            index += 1


# 删除员工信息
class DeleteFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        # 存储需要删除员工的工号
        self.id = tk.StringVar()

        tk.Label(self, text='指定要删除员工的工号', anchor="center", fg="red").grid(row=1, column=1, pady=20, columnspan=2)
        tk.Label(self, text='工号: ').grid(row=2, column=1, pady=40)
        self.e1 = tk.Entry(self, textvariable=self.id)
        self.e1.grid(row=2, column=2)

        tk.Button(self, text='删除', command=self.del_data).grid(row=3, column=2, sticky=tk.E)

    # 删除页面的功能
    def del_data(self):
        self.id = self.e1.get()
        if self.id == "":
            messagebox.showwarning(title='警告信息', message="删除数据为空")
            return
        # 现将所有的数据取出
        db.json_read('staff.json')
        # 取出要删除人的姓名
        c = self.id
        flag = 0
        # 取出json文件中的数据
        d = db.json_load
        for i in d:
          if i['name'] == c:
                flag = 1
                d.remove(i)

        if flag:
            db.json_write('staff.json', d)
            messagebox.showwarning(title='提示信息', message="删除成功")
        else:
            messagebox.showwarning(title='提示信息', message="删除失败,不存在该员工")
        # 清空文本框
        self.e1.delete(0, "end")

# 修改员工信息
class ChangeFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        # 获取插入的数据变量
        self.id = tk.StringVar()
        self.name = tk.StringVar()
        self.price = tk.StringVar()
        self.work = tk.StringVar()
        self.phone = tk.StringVar()
        self.time = tk.StringVar()

        self.del_value = None
        # 页面布局
        tk.Label(self).grid(row=0, column=0)
        # pady是上下边距
        tk.Label(self, text='工号: ').grid(row=1, column=1, pady=10)
        self.e1 = tk.Entry(self, textvariable=self.id)
        self.e1.grid(row=1, column=2)
        tk.Label(self, text='姓名: ').grid(row=2, column=1, pady=10)
        self.e2 = tk.Entry(self, textvariable=self.name)
        self.e2.grid(row=2, column=2)

        tk.Label(self, text='薪资: ').grid(row=3, column=1, pady=10)
        self.e3 = tk.Entry(self, textvariable=self.price)
        self.e3.grid(row=3, column=2)

        tk.Label(self, text='部门: ').grid(row=4, column=1, pady=10)
        self.e4 = tk.Entry(self, textvariable=self.work)
        self.e4.grid(row=4, column=2)

        tk.Label(self, text='电话: ').grid(row=5, column=1, pady=10)
        self.e5 = tk.Entry(self, textvariable=self.phone)
        self.e5.grid(row=5, column=2)

        tk.Label(self, text='入职时间: ').grid(row=6, column=1, pady=10)
        self.e6 = tk.Entry(self, textvariable=self.time)
        self.e6.grid(row=6, column=2)

        tk.Button(self, text='查询', command=self.show_data).grid(row=7, column=1, sticky=tk.E)
        tk.Button(self, text='修改', command=self.modify_data).grid(row=7, column=2, sticky=tk.E)

    # 清空文本框
    def entry_clear(self):
        self.e1.delete(0, tk.END)
        self.e2.delete(0, tk.END)
        self.e3.delete(0, tk.END)
        self.e4.delete(0, tk.END)
        self.e5.delete(0, tk.END)
        self.e6.delete(0, tk.END)

    # 更新文本框内容
    def update_entry(self):
        self.e1.insert(0, self.id)
        self.e2.insert(0, self.name)
        self.e3.insert(0, self.price)
        self.e4.insert(0, self.work)
        self.e5.insert(0, self.phone)
        self.e6.insert(0, self.time)

    # 查询按钮的功能
    def show_data(self):
        self.id = self.e1.get()
        if self.id == "":
            self.entry_clear()
            messagebox.showwarning(title='警告信息', message="查询数据为空")
            return
        db.json_read('staff.json')
        # 取出要查询员工的工号
        c = self.id
        flag = 0
        # 取出json文件中的数据
        self.d = db.json_load
        for i in db.json_load:
            if i['id'] == c:
                flag = 1
                self.del_value = i
                self.id = i['id']
                self.name = i['name']
                self.price = i['price']
                self.work = i['work']
                self.phone = i['phone']
                self.time = i['time']
                self.entry_clear()
                self.update_entry()



        if flag:
            messagebox.showwarning(title='提示', message="查询成功")
        else:
            messagebox.showwarning(title='提示', message="查询失败")

    def modify_data(self):
        self.id = self.e1.get()
        self.name = self.e2.get()
        self.price = self.e3.get()
        self.work = self.e4.get()
        self.phone = self.e5.get()
        self.time = self.e6.get()
        if self.id == "" or self.name == "" or self.price == "" or self.work == "" or self.phone == "" or self.time == "":
            messagebox.showwarning(title='警告信息', message="信息不完整,修改失败")
            return
        if self.del_value == None:
            messagebox.showwarning(title='提示信息', message="还未进行查询不能修改")
            return
        db.json_read('staff.json')
        db.json_load.remove(self.del_value)
        self.del_value = None

        t = {'id': self.id, "name": self.name, "price": self.price, "work": self.work, "phone": self.phone, "time": self.time}
        db.json_insert('staff.json', t)
        self.entry_clear()
        messagebox.showwarning(title='提示信息', message="修改成功")


# 查看员工考勤记录
class AttendFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        colum_names = ['工号', '姓名', '部门','打卡时间']
        self.tree_view = ttk.Treeview(self, show='headings', columns=colum_names)

        # 设置表头
        for colum in colum_names:
            self.tree_view.heading(column=colum, text=colum)

        self.tree_view.tag_configure('even', background='green')  # 声明奇数行的属性
        self.tree_view.tag_configure('odd', background='white')  # 声明偶数行的属性
        # 设置每列的宽度,列中的内容居中
        self.tree_view.column(column=0, width=130, anchor='center')
        self.tree_view.column(column=1, width=130, anchor='center')
        self.tree_view.column(column=2, width=130, anchor='center')
        self.tree_view.column(column=3, width=130, anchor='center')
        self.tree_view.grid(row=0, column=0, columnspan=2)
        # self.show_clear()
        tk.Button(self, text='刷新上班', command=self.up_button).grid(row=3, column=0, sticky=tk.W)
        tk.Button(self, text='刷新下班', command=self.down_button).grid(row=3, column=1, sticky=tk.E)

    # 清空表格中的信息
    def show_clear(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)
    # 刷新上班按钮
    def up_button(self):
        # 每次刷新前先清空一下表格的信息
        self.show_clear()
        # 取出学生信息
        db.json_read('upwork.json')
        index = 0
        for row in db.json_load:
            t = []
            my_tag = 'odd'
            if index % 2 == 0:
                my_tag = 'even'
            for key, value in row.items():
                t.append(value)
            # Treeview中无法拆解字典中的元素,因为字典或元组,是有序的可迭代集合,而字典是一种不可迭代的无序的键值对集合,通过键来访问对应的值，无法像元组和字典那样通过下标迭代访问
            self.tree_view.insert('', "end", values=t, tags=my_tag)
            index += 1

    # 刷新下班按钮
    def down_button(self):
         # 每次刷新前先清空一下表格的信息
        self.show_clear()
        # 取出学生信息
        db.json_read('downwork.json')
        index = 0
        for row in db.json_load:
            t = []
            my_tag = 'odd'
            if index % 2 == 0:
                my_tag = 'even'
            for key, value in row.items():
                t.append(value)
            # Treeview中无法拆解字典中的元素,因为字典或元组,是有序的可迭代集合,而字典是一种不可迭代的无序的键值对集合,通过键来访问对应的值，无法像元组和字典那样通过下标迭代访问
            self.tree_view.insert('', "end", values=t, tags=my_tag)
            index += 1


# 查看员工请假申请
class LeaveFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        colum_names = ['工号', '姓名', '请假理由', '是否批准']
        self.tree_view = ttk.Treeview(self, show='headings', columns=colum_names)

        # 设置表头
        for colum in colum_names:
            self.tree_view.heading(column=colum, text=colum)

        self.tree_view.tag_configure('even', background='green')  # 声明奇数行的属性
        self.tree_view.tag_configure('odd', background='white')  # 声明偶数行的属性
        # 设置每列的宽度,列中的内容居中
        self.tree_view.column(column=0, width=130, anchor='center')
        self.tree_view.column(column=1, width=130, anchor='center')
        self.tree_view.column(column=2, width=130, anchor='center')
        self.tree_view.column(column=3, width=130, anchor='center')
        self.tree_view.grid(row=0, column=0, columnspan=2)
        # self.show_clear()
        self.id = tk.StringVar()
        tk.Label(self, text='请输入工号').grid(row=2, column=0, sticky=tk.E)
        self.e1 = tk.Entry(self, textvariable=self.id)
        self.e1.grid(row=2, column=1, sticky=tk.W)
        tk.Button(self, text='批准', command=self.ok_function).grid(row=2, column=1, sticky=tk.E)
        tk.Button(self, text='刷新', command=self.show_data).grid(row=3, column=1, sticky=tk.E)

    # 清空表格中的信息
    def show_clear(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)

    def entry_clear(self):
        self.e1.delete(0, tk.END)
    # 刷新按钮的功能
    def show_data(self):
        # 每次刷新前先清空一下表格的信息
        self.show_clear()
        #取出学生信息
        db.json_read('leave.json')
        index = 0
        for row in db.json_load:
            t = []
            my_tag = 'odd'
            if index % 2 == 0:
                my_tag = 'even'
            for key, value in row.items():
                t.append(value)
            # Treeview中无法拆解字典中的元素,因为字典或元组,是有序的可迭代集合,而字典是一种不可迭代的无序的键值对集合,通过键来访问对应的值，无法像元组和字典那样通过下标迭代访问
            self.tree_view.insert('', "end", values=t, tags=my_tag)
            index += 1

    # 批准员工请假功能
    def ok_function(self):
        self.id = self.e1.get()

        if self.id == "":
            messagebox.showwarning(title='警告信息', message="要选择一个工号批准请假")
            return
        db.create_json('leave.json')
        db.json_read('leave.json')
        for row in db.json_load:
            if row['id'] == self.id:
                self.name = row['name']
                self.say = row['say']
                self.yon = "已批准"
                db.json_load.remove(row)
                t = {'id': self.id, "name": self.name, "say": self.say, "yon": self.yon}
                db.json_insert('leave.json', t)
                print(db.json_load)
                self.entry_clear()
                messagebox.showwarning(title='提示信息', message="修改成功")
                return

        messagebox.showwarning(title='提示信息', message="修改失败,工号未申请请假")



